﻿using Windows_AI_Assistant.Helpers;
using Betalgo.Ranul.OpenAI.Managers;
using Betalgo.Ranul.OpenAI;
using Betalgo.Ranul.OpenAI.Interfaces;
using Betalgo.Ranul.OpenAI.ObjectModels.RequestModels;
using Windows_AI_Assistant.Services;

namespace Windows_AI_Assistant.ViewModels.Pages
{
    public partial class DashboardViewModel : ObservableObject
    {
        /// <summary>
        /// ini文件路径
        /// </summary>
        static string IniPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.ini");
        /// <summary>
        /// ini文件操作类
        /// </summary>
        private IniFiles Ini = new IniFiles(IniPath);

        [ObservableProperty]
        private string? _apiUrl;

        [ObservableProperty]
        private string? _apiKey;

        [ObservableProperty]
        private string? _apiModel;

        [ObservableProperty]
        private string? _apiPrompt;

        [ObservableProperty]
        private string? _logContent;

        [ObservableProperty]
        private string? _logContentCopy;

        public DashboardViewModel()
        {
            initlizeIniApi();
        }

        /// <summary>
        /// 初始化API设置
        /// </summary>
        private void initlizeIniApi()
        {
            Ini.FindAndCreate(IniPath);
            ApiUrl = Ini.IniReadValue("AISetting", "API_URL");
            ApiKey = Ini.IniReadValue("AISetting", "API_KEY");
            ApiModel = Ini.IniReadValue("AISetting", "API_MODEL");
            ApiPrompt = Ini.IniReadValue("AISetting", "API_PROMPT");
        }

        AIService _aiService = new AIService();

        /// <summary>
        /// 测试API设置
        /// </summary>
        /// <returns></returns>
        [RelayCommand]
        public async Task TestApiSetting()
        {
            LogContentCopy = string.Empty;
            OpenAIService openAIService = new OpenAIService(new OpenAIOptions()
            {
                ApiKey = ApiKey,
                BaseDomain = ApiUrl
            });

            IList<ChatMessage> messages = new List<ChatMessage>{
                new ChatMessage("system", ApiPrompt),
                new ChatMessage("user", "Hello")
            };

            await _aiService.GetChatCompletionAsync(openAIService, ApiModel, messages, OnMessageReceived, OnCompletionReceived);
        }

        /// <summary>
        /// 消息接收事件
        /// </summary>
        /// <param name="message"></param>
        private void OnMessageReceived(string message)
        {
            LogContent = message;
        }

        private void OnCompletionReceived(string completion)
        {
            LogContentCopy += ("~" + completion + "\n");
        }

        /// <summary>
        /// 保存API设置
        /// </summary>
        [RelayCommand]
        public void SaveApiSetting()
        {
            Ini.IniWriteValue("AISetting", "API_URL", ApiUrl);
            Ini.IniWriteValue("AISetting", "API_KEY", ApiKey);
            Ini.IniWriteValue("AISetting", "API_MODEL", ApiModel);
            Ini.IniWriteValue("AISetting", "API_PROMPT", ApiPrompt);
            LogContent = "API设置保存成功！";
        }

    }
}
